- 守护线程 ,就是当有其他线程开启时,守护线程就一直开启
- 守护线程会在所有线程结束后自动结束,
模型
: 人的生命有限 默认100年,上帝会一直存在,所以一直会守护着你
package cn.usts.edu.lesson06;
/**
* 守护线程 ,就是当有其他线程开启时,守护线程就一直开启
* 守护线程会在所有线程结束后自动结束
* */
public class DaemonTest {
public static void main(String[] args) {
= new God();
God god = new You();
You you Thread godThread = new Thread(god);
.setDaemon(true);// 默认是普通用户线程 默认值是false true表示开启守护线程
godThread.start();//守护线程启动
godThread
Thread youThread = new Thread(you);
.start();
youThread}
}
class God implements Runnable{
@Override
public void run() {
while (true){ // 理论上是一直执行,可是当所有线程结束的时候他也会结束.
System.out.println("上帝一直在守护你");
}
}
}
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("快乐的一天");
}
}
}